<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>
<body>

    <div></div>
    <script>
        var div=document.querySelector("div")
        document.onkeydown=function(e)
        {
            console.log(e.key,keyCode);
            var keyCode=e.keyCode||e.which
            var left=div.offsetLeft;
            var top=div.offsetTop;
            if(keyCode==38)
            {
                div.style.top=(top-5)+'px'
            }else if(keyCode==39)
            {
                div.style.left=(left+5)+'px'
            }else if(keyCode==40)
            {
                div.style.top=(top+5)+'px'
            }else if(keyCode==37)
            {
                div.style.left=(left-5)+'px'
            }
        }
    </script>
    
</body>
</html>



